<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <a-button type="primary" preIcon="ant-design:plus-outlined" @click="handleCreate">
          新增
        </a-button>
      </template>
      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              label: '编辑',
              onClick: handleEdit.bind(null, record),
            },
            {
              label: '删除',
              color: 'error',
              popConfirm: {
                title: '是否确认删除',
                placement: 'left',
                confirm: handleDelete.bind(null, record),
              },
            },
          ]"
        />
      </template>
    </BasicTable>
    <tmUserModal @register="registerModal" @success="reload" />
  </div>
</template>

<script lang="ts" name="firstUpperEntityName" setup>
  import { BasicTable, TableAction } from '/@/components/Table';
  import { useJinhuiTable } from '/@/components/Jinhui/hooks/useJinhuiTable';
  import { deleteByIds, getPage } from './tmUser.api';
  import { columns, searchFormSchema } from './tmUser.data';
  import { useModal } from '/@/components/Modal';
  import tmUserModal from './tmUserModal.vue';
  import { message, Modal } from 'ant-design-vue';
  const [registerModal, { openModal }] = useModal();
  const [registerTable, { reload, getSelectRowKeys, clearSelectedRowKeys }, { rowSelection }] =
    useJinhuiTable({
      title: '车厂端用户管理列表',
      api: getPage,
      columns,
      useSearchForm: true,
      actionColumn: {
        width: 150,
        title: '操作',
        dataIndex: 'action',
      },
      beforeFetch: (params) => {
        //根据页数计算start
        let start;
        if (params.start == 1) {
          start = 0;
        } else {
          start = (params.start - 1) * params.limit;
        }
        return {
          'queryData.realName': params.realName,
          'queryData.start': start,
          'queryData.limit': params.limit,
          'queryData.comCode': 1100,
          ...params,
        };
      },
      // afterFetch: (params) => {
      // },
      formConfig: {
        schemas: searchFormSchema,
      },
    });

  function handleCreate() {
    openModal(true, {
      isUpdate: false,
    });
  }

  /**
   * 修改按钮
   * @param record
   */
  function handleEdit(record: Recordable) {
    openModal(true, {
      record,
      isUpdate: true,
    });
  }

  function refresh() {
    Promise.resolve(clearSelectedRowKeys()).then(() => {
      reload();
    });
  }

  async function handleDelete(record: Recordable) {
    await deleteByIds({ selectItems: record.id + ',' }, reload);
  }

  function deleteBatch() {
    const ids = getSelectRowKeys();
    if (null == ids || ids.length == 0) {
      message.warn('请选择数据');
    } else {
      Modal.confirm({
        content: '确定要批量删除吗?',
        onOk() {
          deleteByIds({ selectItems: ids }, refresh);
        },
      });
    }
  }
</script>
